/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.cron.job.stats;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.utils.StringUtil;

/**
 * Esegue una patch per l'indirizzo ip contenuto nei log di salvataggio delle statistiche
 *
 * @author Marco Berri marcoberri@gmail.com
 */
public class PatchIp extends com.cron.job.Base {

    /**
     * parte di esecuzione del cron
     * 
     * @param context
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        super.execute(context);

        debug("Start execute job " + this.getClass().getName());

        String[] table = {"view", "save"};

        for (int i = 0; i < table.length; i++) {
            try {
                Statement stat = this.conn_stats.createStatement();
                ResultSet rsstats = stat.executeQuery("select * from " + table[i] + " order by ts desc");
                while (rsstats.next()) {
                    if (StringUtil.isNullOrEmpty(rsstats.getString("header"))) {
                        continue;
                    }
                    if (!rsstats.getString("ip").equals("127.0.0.1")) {
                        continue;
                    }
                    String ip = null;
                    try {
                        String[] h_string = rsstats.getString("header").split("\\|\\|");
                        for (String s : h_string) {
                            try {
                                if (s.split("\\|")[0].equals("X-Forwarded-For")) {
                                    ip = s.split("\\|")[1];
                                }
                            } catch (Exception e) {
                                fatal("Exception", e);
                                continue;
                            }
                            if (ip == null) {
                                continue;
                            }
                            Statement stat_update = this.conn_stats.createStatement();
                            stat_update.executeQuery("update " + table[i] + " set ip='" + ip + "' where id=" + rsstats.getString("id"));
                        }
                    } catch (Exception ex) {
                        fatal("Exception", ex);
                    }

                }
            } catch (SQLException ex) {
                fatal("SQLException", ex);
            }


        }

        debug("End execute job " + this.getClass().getName());
    }
}
